home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 January / PCWorld_2007-01_cd.bin / v cisle / autoit / autoit-v3.2.0.1-setup.exe / Examples / Helpfile / GUIRegisterMsg.au3 < prev    next >
Text File  |  2006-06-17  |  13KB  |  414 lines

  1. ; *******************************************************
  2. ; Example - Create an ownerdrawn/colored button
  3. ; *******************************************************
  4.  
  5. #include <GUIConstants.au3>
  6.  
  7. Global Const $WM_DRAWITEM            = 0x002B
  8. Global Const $WM_COMMAND            = 0x0111
  9.  
  10. Global Const $GWL_STYLE             = -16
  11.  
  12. Global Const $BS_OWNERDRAW            = 0x0000000B
  13. Global Const $BS_NOTIFY                = 0x00004000
  14.  
  15. Global Const $COLOR_BTNTEXT            = 18
  16. Global Const $COLOR_BTNFACE            = 15
  17. Global Const $COLOR_BTNSHADOW        = 16
  18. Global Const $COLOR_HIGHLIGHTTEXT    = 14
  19. Global Const $COLOR_GRAYTEXT        = 17
  20.  
  21. Global Const $DT_CENTER                = 0x00000001
  22. Global Const $DT_RIGHT                = 0x00000002
  23. Global Const $DT_VCENTER            = 0x00000004
  24. Global Const $DT_BOTTOM                = 0x00000008
  25. Global Const $DT_WORDBREAK            = 0x00000010
  26. Global Const $DT_SINGLELINE            = 0x00000020
  27. Global Const $DT_EXPANDTABS            = 0x00000040
  28. Global Const $DT_TABSTOP            = 0x00000080
  29. Global Const $DT_NOCLIP                = 0x00000100
  30. Global Const $DT_EXTERNALLEADING    = 0x00000200
  31. Global Const $DT_CALCRECT            = 0x00000400
  32. Global Const $DT_NOPREFIX            = 0x00000800
  33. Global Const $DT_INTERNAL            = 0x00001000
  34.  
  35. Global Const $ODS_SELECTED            = 0x0001
  36. Global Const $ODS_GRAYED            = 0x0002
  37. Global Const $ODS_DISABLED            = 0x0004
  38. Global Const $ODS_CHECKED            = 0x0008
  39. Global Const $ODS_FOCUS                = 0x0010
  40. Global Const $ODS_HOTLIGHT            = 0x0040
  41. Global Const $ODS_INACTIVE            = 0x0080
  42. Global Const $ODS_NOACCEL            = 0x0100
  43. Global Const $ODS_NOFOCUSRECT        = 0x0200
  44.  
  45. Global Const $ODT_BUTTON            = 4
  46.  
  47. Global Const $DFC_BUTTON            = 4
  48. Global Const $DFCS_BUTTONPUSH        = 0x0010
  49.  
  50.  
  51. $hGUI        = GUICreate("My Ownerdrawn Created Button", 300, 200)
  52.  
  53. $nButton    = GUICtrlCreateButton("", 90, 50, 120, 30)
  54. GUICtrlSetStyle($nButton, BitOr($WS_TABSTOP, $BS_NOTIFY, $BS_OWNERDRAW)) ; Set the ownerdrawn flag
  55.  
  56. $nButton2    = GUICtrlCreateButton("Normal Button", 90, 110, 120, 30)
  57.  
  58. GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")
  59. ; WM_DRAWITEM has to registered before showing GUI otherwise the initial drawing isn't done
  60. GUIRegisterMsg($WM_DRAWITEM, "MY_WM_DRAWITEM")
  61.  
  62. GUISetState()
  63.  
  64. While 1
  65.     $GUIMsg = GUIGetMsg()
  66.     
  67.     Switch $GUIMsg
  68.         Case $GUI_EVENT_CLOSE
  69.             ExitLoop
  70.             
  71.         Case $nButton
  72.             ; Normally should not run through cause of our MY_WM_COMMAND function
  73.             Msgbox(0, "Info", "Button pressed") 
  74.             
  75.         Case $nButton2
  76.             ; Normally should not run through cause of our MY_WM_COMMAND function
  77.             Msgbox(0, "Info", "Button2 pressed")
  78.     EndSwitch
  79. WEnd
  80.  
  81. Exit
  82.  
  83.  
  84. ; React on a button click
  85. Func MY_WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
  86.     $nNotifyCode    = BitShift($wParam, 16)
  87.     $nID            = BitAnd($wParam, 0x0000FFFF)
  88.     $hCtrl            = $lParam
  89.     
  90.     If $nID <> 2 And $nNotifyCode = 0 Then ; Check for IDCANCEL - 2
  91.         ; Ownerdrawn buttons don't send something by pressing ENTER
  92.         ; So IDOK - 1 comes up, now check for the control that has the current focus
  93.         If $nID = 1 Then
  94.             $hFocus = DllCall("user32.dll", "hwnd", "GetFocus")
  95.             $nCtrlID = DllCall("user32.dll", "int", "GetDlgCtrlID", "hwnd", $hFocus[0])
  96.             PostButtonClick($hFocus[0], $nCtrlID[0])
  97.         Else
  98.             Msgbox(0, "MY_WM_COMMAND", "GUIHWnd" & @TAB & ":" & $hwnd & @LF & _
  99.                                         "MsgID" & @TAB & ":" & $Msg & @LF & _
  100.                                         "wParam" & @TAB & ":" & $wParam & @LF & _
  101.                                         "lParam" & @TAB & ":" & $lParam & @LF & @LF & _
  102.                                         "WM_COMMAND - Infos:" & @LF & _
  103.                                         "-----------------------------" & @LF & _
  104.                                         "Code" & @TAB & ":" & $nNotifyCode & @LF & _
  105.                                         "CtrlID" & @TAB & ":" & $nID & @LF & _
  106.                                         "CtrlHWnd" & @TAB & ":" & $hCtrl)
  107.         EndIf
  108.         Return 0 ; Only workout clicking on the button
  109.     EndIf
  110.     ; Proceed the default Autoit3 internal message commands.
  111.     ; You also can complete let the line out.
  112.     ; !!! But only 'Return' (without any value) will not proceed
  113.     ; the default Autoit3-message in the future !!!
  114.     Return $GUI_RUNDEFMSG
  115. EndFunc
  116.  
  117.  
  118. ; RePost a WM_COMMAND message to a ctrl in a gui window
  119. Func PostButtonClick($hWnd, $nCtrlID)
  120.     DllCall("user32.dll", "int", "PostMessage", _
  121.                                     "hwnd", $hGUI, _
  122.                                     "int", $WM_COMMAND, _
  123.                                     "int", BitAnd($nCtrlID, 0x0000FFFF), _
  124.                                     "hwnd", GUICtrlGetHandle($nCtrlid))
  125. EndFunc
  126.  
  127.  
  128. ; Draw the button
  129. Func MY_WM_DRAWITEM($hWnd, $Msg, $wParam, $lParam)
  130.     Local $stDrawItem = DllStructCreate("uint;uint;uint;uint;uint;uint;uint;int[4];dword", $lParam)
  131.     
  132.     $nCtlType = DllStructGetData($stDrawItem, 1)
  133.     If $nCtlType = $ODT_BUTTON Then
  134.         $nCtrlID    = DllStructGetData($stDrawItem, 2)
  135.         $nItemState    = DllStructGetData($stDrawItem, 5)
  136.         $hCtrl        = DllStructGetData($stDrawItem, 6)
  137.         $hDC        = DllStructGetData($stDrawItem, 7)
  138.         $nLeft        = DllStructGetData($stDrawItem, 8, 1)
  139.         $nTop        = DllStructGetData($stDrawItem, 8, 2)
  140.         $nRight        = DllStructGetData($stDrawItem, 8, 3)
  141.         $nBottom    = DllStructGetData($stDrawItem, 8, 4)
  142.         $sText        = "Ownerdrawn Button"
  143.         $nTextColor    = 0x5555DD
  144.         $nBackColor    = 0xFFEEDD
  145.         DrawButton($hWnd, $hCtrl, $hDC, $nLeft, $nTop, $nRight, $nBottom, $nItemState, $sText, $nTextColor, $nBackColor)
  146.         $stDrawItem = 0
  147.         Return 1
  148.     EndIf
  149.  
  150.     $stDrawItem = 0
  151.     Return $GUI_RUNDEFMSG ; Proceed the default Autoit3 internal message commands
  152. EndFunc
  153.  
  154.  
  155. ; The main drawing procedure
  156. Func DrawButton($hWnd, $hCtrl, $hDC, $nLeft, $nTop, $nRight, $nBottom, $nItemState, $sText, $nTextColor, $nBackColor)
  157.     ;Local $bDefault    = FALSE
  158.     Local $bChecked    = BitAnd($nItemState, $ODS_CHECKED)
  159.     Local $bFocused    = BitAnd($nItemState, $ODS_FOCUS)
  160.     Local $bGrayed    = BitAnd($nItemState, BitOr($ODS_GRAYED, $ODS_DISABLED))
  161.     Local $bSelected= BitAnd($nItemState, $ODS_SELECTED)
  162.  
  163.     $stRect = DllStructCreate("int;int;int;int")
  164.     DllStructSetData($stRect, 1, $nLeft)
  165.     DllStructSetData($stRect, 2, $nTop)
  166.     DllStructSetData($stRect, 3, $nRight)
  167.     DllStructSetData($stRect, 4, $nBottom)
  168.     
  169.     If $bGrayed Then
  170.         $nClrTxt    = SetTextColor($hDC, GetSysColor($COLOR_HIGHLIGHTTEXT))
  171.     ElseIf $nTextColor = -1 Then
  172.         $nClrTxt    = SetTextColor($hDC, GetSysColor($COLOR_BTNTEXT))
  173.     Else
  174.         $nClrTxt    = SetTextColor($hDC, $nTextColor)
  175.     EndIf
  176.     
  177.     If $nBackColor = -1 Then
  178.         $hBrush        = GetSysColorBrush($COLOR_BTNFACE)
  179.         $nClrSel    = GetSysColor($COLOR_BTNFACE)
  180.     Else
  181.         $hBrush        = CreateSolidBrush($nBackColor)
  182.         $nClrSel    = $nBackColor;
  183.     EndIf
  184.  
  185.     $nClrBk            = SetBkColor($hDC, $nClrSel)
  186.     $hOldBrush        = SelectObject($hDC, $hBrush)
  187.  
  188.     $nTmpLeft    = $nLeft
  189.     $nTmpTop    = $nTop
  190.     $nTmpRight    = $nRight
  191.     $nTmpBottom    = $nBottom
  192.     
  193.     If $bSelected Then
  194.         InflateRect($nTmpLeft, $nTmpTop, $nTmpRight, $nTmpBottom, -1, -1)
  195.         $hBrushSel = CreateSolidBrush(GetSysColor($COLOR_BTNSHADOW))
  196.         FrameRect($hDC, $nTmpLeft, $nTmpTop, $nTmpRight, $nTmpBottom, $hBrushSel)
  197.         DeleteObject($hBrushSel)
  198.     Else
  199.         If $bFocused And Not $bSelected Then InflateRect($nTmpLeft, $nTmpTop, $nTmpRight, $nTmpBottom, -1, -1)
  200.         DrawFrameControl($hDC, $nTmpLeft, $nTmpTop, $nTmpRight, $nTmpBottom, $DFC_BUTTON, $DFCS_BUTTONPUSH)
  201.     EndIf
  202.     
  203.     $nTmpLeft    = $nLeft
  204.     $nTmpTop    = $nTop
  205.     $nTmpRight    = $nRight
  206.     $nTmpBottom    = $nBottom
  207.     
  208.     If $bSelected Then
  209.         InflateRect($nTmpLeft, $nTmpTop, $nTmpRight, $nTmpBottom, -2, -2)
  210.     Else
  211.         If $bFocused And Not $bSelected Then
  212.             InflateRect($nTmpLeft, $nTmpTop, $nTmpRight, $nTmpBottom, -3, -3)
  213.             $nTmpLeft -= 1
  214.             $nTmpTop -= 1
  215.         Else
  216.             InflateRect($nTmpLeft, $nTmpTop, $nTmpRight, $nTmpBottom, -2, -2)
  217.             $nTmpLeft -= 1
  218.             $nTmpTop -= 1
  219.         EndIf
  220.     EndIf
  221.     
  222.     FillRect($hDC, $nTmpLeft, $nTmpTop, $nTmpRight, $nTmpBottom, $hBrush)
  223.     
  224.     If $bSelected Or $bGrayed Then
  225.         $nTmpLeft = $nTmpLeft + 2
  226.         $nTmpTop = $nTmpTop +2
  227.     EndIf
  228.     
  229.     $uFlags = BitOr($DT_NOCLIP, $DT_CENTER, $DT_VCENTER)
  230.  
  231.     If Not BitAnd(GetWindowLong($hCtrl, $GWL_STYLE), $BS_MULTILINE) Then $uFlags = BitOr($uFlags, $DT_SINGLELINE)
  232.     
  233.     DrawText($hDC, $sText, $nTmpLeft, $nTmpTop, $nTmpRight, $nTmpBottom, $uFlags)
  234.  
  235.     If $bGrayed Then
  236.         $nTmpLeft    = $nLeft
  237.         $nTmpTop    = $nTop
  238.         $nTmpRight    = $nRight
  239.         $nTmpBottom    = $nBottom
  240.         
  241.         $nTmpLeft -= 1
  242.         
  243.         $nClrTxt    = SetTextColor($hDC, GetSysColor($COLOR_GRAYTEXT))
  244.         DrawText($hDC, $sText, $nTmpLeft, $nTmpTop, $nTmpRight, $nTmpBottom, BitOr($DT_NOCLIP, $DT_CENTER, $DT_VCENTER, $DT_SINGLELINE))
  245.     EndIf
  246.  
  247.     $nTmpLeft    = $nLeft
  248.     $nTmpTop    = $nTop
  249.     $nTmpRight    = $nRight
  250.     $nTmpBottom    = $nBottom
  251.         
  252.     If $bFocused Then
  253.         $hBrush    = CreateSolidBrush(0)
  254.         FrameRect($hDC, $nTmpLeft, $nTmpTop, $nTmpRight, $nTmpBottom, $hBrush)
  255.         
  256.         $nTmpLeft    = $nLeft
  257.         $nTmpTop    = $nTop
  258.         $nTmpRight    = $nRight
  259.         $nTmpBottom    = $nBottom
  260.         
  261.         InflateRect($nTmpLeft, $nTmpTop, $nTmpRight, $nTmpBottom, -4, -4)
  262.         DrawFocusRect($hDC, $nTmpLeft, $nTmpTop, $nTmpRight, $nTmpBottom)
  263.     EndIf
  264.  
  265.     SelectObject($hDC, $hOldBrush)
  266.     DeleteObject($hBrush)
  267.     SetTextColor($hDC, $nClrTxt)
  268.     SetBkColor($hDC, $nClrBk)
  269.          
  270.     Return 1
  271. EndFunc
  272.  
  273.  
  274. ; Some graphic / windows functions
  275. Func CreateSolidBrush($nColor)
  276.     Local $hBrush = DllCall("gdi32.dll", "hwnd", "CreateSolidBrush", "int", $nColor)
  277.     Return $hBrush[0]
  278. EndFunc
  279.  
  280.  
  281. Func GetSysColor($nIndex)
  282.     Local $nColor = DllCall("user32.dll", "int", "GetSysColor", "int", $nIndex)
  283.     Return $nColor[0]
  284. EndFunc
  285.  
  286.  
  287. Func GetSysColorBrush($nIndex)
  288.     Local $hBrush = DllCall("user32.dll", "hwnd", "GetSysColorBrush", "int", $nIndex)
  289.     Return $hBrush[0]
  290. EndFunc
  291.  
  292.  
  293. Func DrawFrameControl($hDC, $nLeft, $nTop, $nRight, $nBottom, $nType, $nState)
  294.     Local     $stRect = DllStructCreate("int;int;int;int")
  295.     
  296.     DllStructSetData($stRect, 1, $nLeft)
  297.     DllStructSetData($stRect, 2, $nTop)
  298.     DllStructSetData($stRect, 3, $nRight)
  299.     DllStructSetData($stRect, 4, $nBottom)
  300.     
  301.     DllCall("user32.dll", "int", "DrawFrameControl", "hwnd", $hDC, "ptr", DllStructGetPtr($stRect), "int", $nType, "int", $nState)
  302.  
  303.     $stRect = 0
  304. EndFunc
  305.  
  306.  
  307. Func DrawFocusRect($hDC, $nLeft, $nTop, $nRight, $nBottom)
  308.     Local     $stRect = DllStructCreate("int;int;int;int")
  309.     
  310.     DllStructSetData($stRect, 1, $nLeft)
  311.     DllStructSetData($stRect, 2, $nTop)
  312.     DllStructSetData($stRect, 3, $nRight)
  313.     DllStructSetData($stRect, 4, $nBottom)
  314.     
  315.     DllCall("user32.dll", "int", "DrawFocusRect", "hwnd", $hDC, "ptr", DllStructGetPtr($stRect))
  316.     
  317.     $stRect = 0
  318. EndFunc
  319.  
  320.  
  321. Func DrawText($hDC, $sText, $nLeft, $nTop, $nRight, $nBottom, $nFormat)
  322.     Local $nLen = StringLen($sText)
  323.     
  324.     Local $stRect = DllStructCreate("int;int;int;int")
  325.     DllStructSetData($stRect, 1, $nLeft)
  326.     DllStructSetData($stRect, 2, $nTop)
  327.     DllStructSetData($stRect, 3, $nRight)
  328.     DllStructSetData($stRect, 4, $nBottom)
  329.     
  330.     Local $stText = DllStructCreate("char[260]")
  331.     DllStructSetData($stText, 1, $sText)
  332.     
  333.     DllCall("user32.dll", "int", "DrawText", "hwnd", $hDC, "ptr", DllStructGetPtr($stText), "int", $nLen, "ptr", DllStructGetPtr($stRect), "int", $nFormat)
  334.     
  335.     $stRect = 0
  336.     $stText    = 0
  337. EndFunc
  338.  
  339.  
  340. Func FillRect($hDC, $nLeft, $nTop, $nRight, $nBottom, $hBrush)
  341.     Local     $stRect = DllStructCreate("int;int;int;int")
  342.     
  343.     DllStructSetData($stRect, 1, $nLeft)
  344.     DllStructSetData($stRect, 2, $nTop)
  345.     DllStructSetData($stRect, 3, $nRight)
  346.     DllStructSetData($stRect, 4, $nBottom)
  347.     
  348.     DllCall("user32.dll", "int", "FillRect", "hwnd", $hDC, "ptr", DllStructGetPtr($stRect), "hwnd", $hBrush)
  349.     
  350.     $stRect = 0
  351. EndFunc
  352.  
  353.  
  354. Func FrameRect($hDC, $nLeft, $nTop, $nRight, $nBottom, $hBrush)
  355.     Local     $stRect = DllStructCreate("int;int;int;int")
  356.     
  357.     DllStructSetData($stRect, 1, $nLeft)
  358.     DllStructSetData($stRect, 2, $nTop)
  359.     DllStructSetData($stRect, 3, $nRight)
  360.     DllStructSetData($stRect, 4, $nBottom)
  361.     
  362.     DllCall("user32.dll", "int", "FrameRect", "hwnd", $hDC, "ptr", DllStructGetPtr($stRect), "hwnd", $hBrush)
  363.     
  364.     $stRect = 0
  365. EndFunc
  366.  
  367.  
  368. Func InflateRect(ByRef $nLeft, ByRef $nTop, ByRef $nRight, ByRef $nBottom, $nX, $nY)
  369.     Local     $stRect = DllStructCreate("int;int;int;int")
  370.     
  371.     DllStructSetData($stRect, 1, $nLeft)
  372.     DllStructSetData($stRect, 2, $nTop)
  373.     DllStructSetData($stRect, 3, $nRight)
  374.     DllStructSetData($stRect, 4, $nBottom)
  375.     
  376.     DllCall("user32.dll", "int", "InflateRect", "ptr", DllStructGetPtr($stRect), "int", $nX, "int", $nY)
  377.     
  378.     $nLeft        = DllStructGetData($stRect, 1)
  379.     $nTop        = DllStructGetData($stRect, 2)
  380.     $nRight        = DllStructGetData($stRect, 3)
  381.     $nBottom    = DllStructGetData($stRect, 4)
  382.     
  383.     $stRect = 0
  384. EndFunc
  385.  
  386.  
  387. Func SetBkColor($hDC, $nColor)
  388.     Local $nOldColor = DllCall("gdi32.dll", "int", "SetBkColor", "hwnd", $hDC, "int", $nColor)
  389.     Return $nOldColor[0]
  390. EndFunc
  391.  
  392.  
  393. Func SetTextColor($hDC, $nColor)
  394.     Local $nOldColor = DllCall("gdi32.dll", "int", "SetTextColor", "hwnd", $hDC, "int", $nColor)
  395.     Return $nOldColor[0]
  396. EndFunc
  397.  
  398.  
  399. Func SelectObject($hDC, $hObj)
  400.     Local $hOldObj = DllCall("gdi32.dll", "hwnd", "SelectObject", "hwnd", $hDC, "hwnd", $hObj)
  401.     Return $hOldObj[0]
  402. EndFunc
  403.  
  404.  
  405. Func DeleteObject($hObj)
  406.     Local $nResult = DllCall("gdi32.dll", "hwnd", "DeleteObject", "hwnd", $hObj)
  407. EndFunc
  408.  
  409.  
  410. Func GetWindowLong($hWnd, $nIndex)
  411.     Local $nVal = DllCall("user32.dll", "int", "GetWindowLong", "hwnd", $hWnd, "int", $nIndex)
  412.     Return $nVal[0]
  413. EndFunc
  414.